﻿using System;
using System.Collections;
using System.Linq;

namespace Program19
{
    class Program
    {
        static void Main(string[] args)
        {
            //方法一
            //int[] ar1 = { 1, 2, 3, 4, 12 };
            //int[] ar2 = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            //int[] ar3 = ar1.Union(ar2).ToArray();

            //for (var i = 0; i < ar3.Length; i++)
            //{
            //    Console.Write(ar3[i]+" ");
            //}
            //Console.WriteLine();
            //Array.Sort(ar3);
            //Console.WriteLine("排序后...");
            //for (var i = 0; i < ar3.Length; i++)
            //{
            //    Console.Write(ar3[i] + " ");
            //}

            //方法二
            int[] nums = new int[10];
            int[] nums1 = new int[6];
            #region 重复元素判断
            ArrayList arrayList = new ArrayList();
            Random md = new Random();

            Console.WriteLine("初始化数组A");
            while (arrayList.Count < 10)
            {
                int tempNum = md.Next(1, 100);
                if (!arrayList.Contains(tempNum))//判断元素是否存在arrayList中（避免不重复）
                {
                    arrayList.Add(tempNum);
                }
            }
            for (var i = 0; i < nums.Length; i++)
            {
                nums[i] = (int)arrayList[i];
                Console.Write(nums[i] + " ");
            }

            Console.WriteLine();
            Console.WriteLine("初始化数组B");
            
            while (arrayList.Count < 6)
            {
                int tempNum = md.Next(1, 100);
                if (!arrayList.Contains(tempNum))//判断元素是否存在arrayList中（避免不重复）
                {
                    arrayList.Add(tempNum);
                }
            }
            for (var i = 0; i < nums1.Length; i++)
            {
                nums1[i] = (int)arrayList[i];
                Console.Write(nums1[i] + " ");
            }

            //合并两个数组
            int[] result = new int[20];
            //1.把nums依次放入result[0-9]
            for(var i=0;i<nums.Length;i++)
            {
                result[i] = nums[i];
            }
            //2.把nums1依次放入result[10-15]
            for (var i = 0; i < nums1.Length; i++)
            {
                result[i+10] = nums1[i];
            }
            
            Console.WriteLine();
            Console.WriteLine("合并AB的结果");
            for (int i = 0; i < nums.Length + nums1.Length; i++)
            {
 
                        Console.Write(result[i] + " ");

            }
            
            //合并数组去重复
            Console.WriteLine();
            Console.WriteLine("合并数组去重复");
            for (int i = 0; i < (nums.Length + nums1.Length) - 1; i++)
            {
                int n1 = i;
                for (int j = i + 1; j < nums.Length + nums1.Length; j++)
                {
                    if (result[j] < result[n1])
                    {
                        n1 = j;
                    }
                }
                var tmp = result[i];
                result[i] = result[n1];
                result[n1] = tmp;
            }
            for (int i = 0; i < nums.Length + nums1.Length; i++)
            { 
                if(!(result[i]==result[i+1]))
                {
                    Console.Write(result[i] + " ");
                }
            }
                #endregion


            }
    }
}
